package com.funtl.screw.codegen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.codegen.entity.GenDatasourceConf;
import com.funtl.screw.codegen.service.GenDatasourceConfService;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import com.funtl.screw.common.security.annotation.Inner;
import com.funtl.screw.common.xss.core.XssCleanIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据源管理
 *
 * @author Lusifer
 * @since 2023-03-31 16:00:20
 */
@RestController
@AllArgsConstructor
@Api(value = "datasource", tags = "数据源管理")
@RequestMapping("/dsconf")
public class GenDsConfController {

    private final GenDatasourceConfService datasourceConfService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public ResponseResult<IPage<GenDatasourceConf>> getSysDatasourceConfPage(QueryRequest queryRequest, GenDatasourceConf datasourceConf) {
        Page<GenDatasourceConf> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        return ResponseResult.ok(datasourceConfService.page(page, Wrappers.query(datasourceConf)));
    }

    /**
     * 查询全部数据源
     */
    @ApiOperation(value = "查询全部数据源")
    @GetMapping("/list")
    @Inner(value = false)
    public ResponseResult<List<GenDatasourceConf>> list() {
        return ResponseResult.ok(datasourceConfService.list());
    }

    /**
     * 通过id查询数据源表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询数据源表")
    @GetMapping("/{id}")
    public ResponseResult<GenDatasourceConf> getById(@PathVariable("id") Long id) {
        return ResponseResult.ok(datasourceConfService.getById(id));
    }

    /**
     * 新增数据源表
     *
     * @param datasourceConf 数据源表
     * @return R
     */
    @ApiOperation(value = "新增数据源表")
    @SysLog("新增数据源表")
    @PostMapping
    @XssCleanIgnore
    public ResponseResult<Boolean> save(@RequestBody GenDatasourceConf datasourceConf) {
        return ResponseResult.ok(datasourceConfService.saveDsByEnc(datasourceConf));
    }

    /**
     * 修改数据源表
     *
     * @param conf 数据源表
     * @return R
     */
    @ApiOperation(value = "修改数据源表")
    @SysLog("修改数据源表")
    @PutMapping
    @XssCleanIgnore
    public ResponseResult<Boolean> updateById(@RequestBody GenDatasourceConf conf) {
        return ResponseResult.ok(datasourceConfService.updateDsByEnc(conf));
    }

    /**
     * 通过id删除数据源表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除数据源表")
    @SysLog("删除数据源表")
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> removeById(@PathVariable Long id) {
        return ResponseResult.ok(datasourceConfService.removeByDsId(id));
    }

}
